﻿using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using WebCoreAPI.Identity;

namespace WebCoreAPI.Config
{
    public class IdentityInjectConfig
    {
        public static void InjectConfig(WebApplicationBuilder builder)
        {
            IServiceCollection services = builder.Services;
            string? mySqlConStr = builder.Configuration.GetSection("MySqlConStr").Value;
            services.AddDbContext<MyDbContext>(opt =>
            {
                opt.UseMySql(mySqlConStr, ServerVersion.AutoDetect(mySqlConStr))
           .EnableDetailedErrors().EnableSensitiveDataLogging();
            });
            services.AddDataProtection();  //Protection 保护
            services.AddIdentityCore<MyUser>(opt =>
            {     //还有一个AddIdentity是给MVC用的，会有页面

                opt.Lockout.MaxFailedAccessAttempts = 10;  //密码验证失败10则锁定
                opt.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromSeconds(10);  //锁定时间

                opt.Password.RequireDigit = false;  //Digit 数字
                opt.Password.RequireLowercase = false;  //小写
                opt.Password.RequireUppercase = false;  //大写
                opt.Password.RequiredLength = 6;
                opt.Password.RequireNonAlphanumeric = false;  //Alphanumeric 字母和数字
                opt.Tokens.PasswordResetTokenProvider = TokenOptions.DefaultEmailProvider; //这种配置验证码短少，不配置验证码大
                opt.Tokens.EmailConfirmationTokenProvider = TokenOptions.DefaultEmailProvider;
            });
            var idBuilder = new IdentityBuilder(typeof(MyUser), typeof(MyRole), services);
            idBuilder.AddEntityFrameworkStores<MyDbContext>()
                .AddDefaultTokenProviders()
                .AddRoleManager<RoleManager<MyRole>>()
                .AddUserManager<UserManager<MyUser>>();

        }

    }
}
